package cn.xxstudy.compose.material

import android.os.Bundle
import android.util.Log
import androidx.activity.compose.setContent
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.*
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import cn.xxstudy.compose.ui.theme.ComposeDemoTheme
import kotlinx.coroutines.launch

/**
 * @author ：wuxinxi on 2021/3/8 .
 * @packages ：cn.xxstudy.compose.material .
 * TODO:一句话描述
 */
class SimpleListItemActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            ComposeDemoTheme {
                DefaultPreview()
            }
        }
    }
}

@Preview
@Composable
fun DefaultPreview() {
    val scope = rememberCoroutineScope()
    val activity = LocalContext.current as SimpleListItemActivity
    val scaffoldState = rememberScaffoldState()
    Scaffold(
        scaffoldState = scaffoldState,
        topBar = {
            TopAppBar(title = { Text(text = "ListItem") }, navigationIcon = {
                IconButton(onClick = { scope.launch { activity.finish() } }) {
                    Icon(Icons.Default.ArrowBack, contentDescription = "")
                }
            })
        },
        content = { ListItemContent(scaffoldState) }
    )
}

@Composable
fun ListItemContent(state: ScaffoldState) {
    LazyColumn(content = {
        item { OneLineItems(state) }
        item { Divider() }
        item { TwoLineItems(state) }
    })
}

@OptIn(ExperimentalMaterialApi::class)
@Composable
fun OneLineItems(state: ScaffoldState) {
    val scope = rememberCoroutineScope()
    var clickCount by remember { mutableStateOf(0) }

    Column {
        ListItem(text = { Text(text = "One line list item with no icon") })
        Divider()
        ListItem(
            text = { Text("One line list item with 24*24 icon") },
            icon = { Icon(Icons.Default.Favorite, "") })
        Divider()
        ListItem(
            text = { Text("One line list item with 56*56 icon") },
            icon = { Icon(Icons.Default.Favorite, "", modifier = Modifier.size(54.dp)) })
        Divider()
        ListItem(
            text = { Text("One line clickable list item") },
            icon = {
                Icon(
                    Icons.Default.Person,
                    contentDescription = null,
                    modifier = Modifier.size(56.dp)
                )
            },
            modifier = Modifier.clickable {
                scope.launch {
                    state.snackbarHostState.showSnackbar("click=${++clickCount}")
                }
            }
        )
        Divider()
        ListItem(
            text = { Text("One line list item with trailing icon") },
            trailing = { Icon(Icons.Default.Favorite, "") }
        )
        Divider()
        ListItem(
            text = { Text("One line  list item") },
            icon = {
                Icon(
                    Icons.Default.Person,
                    contentDescription = null,
                    modifier = Modifier.size(56.dp)
                )
            },
            trailing = { Icon(Icons.Default.Favorite, "") },
            modifier = Modifier.clickable {
                scope.launch {
                    state.snackbarHostState.showSnackbar("click=${++clickCount}")
                }
            }
        )
    }
}


@OptIn(ExperimentalMaterialApi::class)
@Composable
fun TwoLineItems(state: ScaffoldState) {
    val scope = rememberCoroutineScope()
    var clickCount by remember { mutableStateOf(0) }

    Column {
        ListItem(
            text = { Text(text = "Two line list item with no icon") },
            secondaryText = { Text(text = "列表项的辅助文本") }
        )
        Divider()
        ListItem(
            icon = { Icon(Icons.Default.Favorite, "") },
            text = { Text(text = "Two line list item with no icon") },
            singleLineSecondaryText = false,
            secondaryText = { Text(text = "列表项的辅助文本->非单行-非单行非单行非单行非单行非单行非单行非单行非单行非单行非单行非单行非单行非单行非单行") },
            trailing = { Icon(Icons.Default.Menu, "") }
        )
        Divider()
        ListItem(
            text = { Text(text = "Two line list item with no icon") },
            singleLineSecondaryText = true,
            secondaryText = { Text(text = "列表项的辅助文本->单行-单行单行单行单行单行单行单行单行单行单行单行单行单行单行") },
            icon = { Icon(Icons.Default.Favorite, "") },
            trailing = { Text("trailing Text") }

        )
        Divider()
        ListItem(
            text = { Text("One line list item with 24*24 icon") },
            overlineText = { Text(text = "显示在主文本上方的文本") },
            icon = { Icon(Icons.Default.Favorite, "") })
        Divider()
        ListItem(
            text = { Text("One line list item with 56*56 icon") },
            overlineText = { Text(text = "显示在主文本上方的文本") },
            secondaryText = { Text(text = "列表项的辅助文本") },
            icon = { Icon(Icons.Default.Favorite, "", modifier = Modifier.size(54.dp)) })
    }
}